package cn.wp.org.service.impl;

import cn.wp.basic.constant.PetHomeConstant;
import cn.wp.basic.exception.CustomException;
import cn.wp.basic.service.impl.BaseServiceImpl;
import cn.wp.basic.util.MD5Utils;
import cn.wp.basic.util.StrUtils;
import cn.wp.org.domain.Employee;
import cn.wp.org.domain.Reject;
import cn.wp.org.domain.Shop;
import cn.wp.org.mapper.EmployeeMapper;
import cn.wp.org.mapper.ShopMapper;
import cn.wp.org.service.IShopService;
import cn.wp.user.domain.LoginInfo;
import cn.wp.user.mapper.LoginInfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.List;

@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private LoginInfoMapper loginInfoMapper;

    @Autowired
    private JavaMailSender javaMailSender;

    @Override
    public void patchDel(List<Long> ids) {
        shopMapper.patchDel(ids);
    }

    @Override
    @Transactional
    public void settlement(Shop shop) throws CustomException {
        // 校验前端传递的数据
        checkData(shop);
        // 设置动态盐值

        // 保存店铺
        shopMapper.save(shop);
        // 获取员工对象
        Employee admin = shop.getAdmin();
        // 设置动态盐值
        admin.setSalt(StrUtils.getComplexRandomString(10));
        // 密码加密
        admin.setPassword(MD5Utils.encrypByMd5(admin.getPassword()+admin.getSalt()));
        // 获取管理员信息
        LoginInfo loginInfo = getLoginInfo(admin);
        loginInfoMapper.save(loginInfo);

        // 给员工设置店铺
        admin.setShop(shop);

        admin.setLoginInfo(loginInfo);
        employeeMapper.save(admin);
        // 修改店铺
        shopMapper.update(shop);
    }

    private LoginInfo getLoginInfo(Employee admin) {
        LoginInfo loginInfo = new LoginInfo();
        BeanUtils.copyProperties(admin, loginInfo);
        loginInfo.setType(PetHomeConstant.ADMIN);
        return loginInfo;
    }

    @Override
    @Transactional
    public void passShop(Shop shop) throws MessagingException {
        // 创建复杂邮箱类
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
        // 发件人
        mimeMessageHelper.setFrom("2569911437@qq.com");
        // 邮件标题
        mimeMessageHelper.setSubject("店铺激活！");
        // 邮件类容，连接地址为门店接口的activation，get传递门店id，用于更改门店状态
        mimeMessageHelper.setText("<a href='http://localhost/shop/activation/?id="+shop.getId()+"'>点击激活店铺</a>", true);

        // 通过Admin获取到id，通过id查询邮箱
        Long admin_id = shop.getAdmin().getId();
        Employee employee = employeeMapper.loadOne(admin_id);

        // 收件人
        mimeMessageHelper.setTo(employee.getEmail());
        // 发送邮件
        javaMailSender.send(mimeMessage);
        // 设置状态
        shop.setState(1);
        // 更新
        shopMapper.update(shop);

    }

    @Override
    @Transactional
    public void activation(Long id) {
        // 通过id查询要激活的门店
        Shop shop = shopMapper.loadOne(id);
        // 设置门店状态
        shop.setState(2);
        // 更新
        shopMapper.update(shop);
    }

    @Override
    public void reject(Shop shop) {
        // 创建一个驳回信息类
        Reject reject = new Reject();
        // 将前台传入的驳回信息，设置到content字段中
        reject.setContent(shop.getRejectContent());
        // 将前台传入的门店id，设置到shop_id中去
        reject.setShop_id(shop.getId());
        // 保存驳回信息
        shopMapper.addReject(reject);
        // 设置门店状态
        shop.setState(-1);
        // 更新
        shopMapper.update(shop);
    }

    private void checkData(Shop shop) throws CustomException {
        if (StringUtils.isEmpty(shop.getName())) {
            throw new CustomException("店铺名称不能为空");
        }
        if (StringUtils.isEmpty(shop.getTel())) {
            throw new CustomException("店铺号码不能为空");
        }
        if (StringUtils.isEmpty(shop.getAddress())) {
            throw new CustomException("店铺地址不能为空");
        }
        if (StringUtils.isEmpty(shop.getAdmin().getUsername())) {
            throw new CustomException("店铺管理人员名不能为空");
        }
        if (StringUtils.isEmpty(shop.getAdmin().getPhone())) {
            throw new CustomException("手机不能为空");
        }
        if (StringUtils.isEmpty(shop.getAdmin().getEmail())) {
            throw new CustomException("邮箱不能为空");
        }
        if(StringUtils.isEmpty(shop.getAdmin().getPassword())){
            throw new CustomException("密码不能为空");
        }
        // 根据店铺名查询店铺对象
        Shop s = shopMapper.loadByName(shop.getName());
        if(s!=null){
            throw new CustomException("店铺名已存在");
        }
        Employee employee = employeeMapper.loadByUsername(shop.getAdmin().getUsername());
        if (employee != null) {
            throw new CustomException("用户名已存在");
        }
    }

}
